/*
    Author:     Adam
    Github:     https://gitee.com/AdamLoong

    Version:    1.0.0

    MIT License

    Copyright (c) 2025 AdamLoong

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
*/
#include "free_button.h"

static Button *btn_list_head = NULL;

static void (*button_event_callback)(Button *btn, uint8_t event) = NULL;

static void button_handler(Button *btn);

void button_tick(void)
{
#if 1
    static uint32_t count = 0;
    if (++count < BTN_TICK_INTERVAL)
    {
        return;
    }
    count = 0;
#endif

    Button *btn;
    for (btn = btn_list_head; btn; btn = btn->next)
    {
        button_handler(btn);
    }
}

void button_init(Button *btn, BtnStatus (*read_status)(void *arg), void *arg)
{
    btn->read_status = read_status;
    btn->arg = arg;
    btn->state = BTN_FLAG_IDLE;
    btn->event = 0;

    Button *p = btn_list_head;
    while (p)
    {
        if (p == btn)
            return;
        p = p->next;
    }

    btn->next = btn_list_head;
    btn_list_head = btn;
}

bool button_event_acquire(Button *btn, uint8_t event)
{
    if ((event & btn->event) == event)
    {
        btn->event &= ~event;
        button_clear_all_btn(); // Beta
        return true;
    }
    else
    {
        return false;
    }
}

void button_clear_all_btn(void)
{
    Button *btn;
    for (btn = btn_list_head; btn; btn = btn->next)
    {
        btn->event = 0;
    }
}

void button_register_event_callback(void (*event_callback)(Button *btn, uint8_t event))
{
    button_event_callback = event_callback;
}

static void button_event_release(Button *btn, uint8_t event)
{
    btn->event |= event;

    if (button_event_callback)
    {
        button_event_callback(btn, event);
    }
}

static void button_handler(Button *btn)
{
    int btn_status = btn->read_status(btn->arg);

    if (btn->state != BTN_FLAG_IDLE)
    {
        btn->tick += BTN_TICK_INTERVAL;
    }

    switch (btn->state)
    {
    case BTN_FLAG_IDLE:
        if (btn_status == BTN_STATUS_PRESS_DOWN)
        {
            button_event_release(btn, BTN_FLAG_PRESS_DOWN);
            btn->state = BTN_FLAG_PRESS_DOWN;
            btn->tick = 0;
            btn->click_count = 1;
        }
        btn->event |= BTN_FLAG_IDLE;
        break;

    case BTN_FLAG_PRESS_DOWN:
        if (btn_status == BTN_STATUS_PRESS_UP)
        {
            button_event_release(btn, BTN_FLAG_PRESS_UP);
            btn->state = BTN_FLAG_PRESS_UP;
            btn->tick = 0;
        }
        else if (btn->tick > BTN_TIMEOUT_LONG_PRESS_START)
        {
            button_event_release(btn, BTN_FLAG_LONG_PRESS_START);
            btn->state = BTN_FLAG_LONG_PRESS_START;
        }
        break;

    case BTN_FLAG_PRESS_UP:
        if (btn_status == BTN_STATUS_PRESS_DOWN)
        {
            button_event_release(btn, BTN_FLAG_PRESS_DOWN);
            button_event_release(btn, BTN_FLAG_PRESS_REPEAT);
            btn->state = BTN_FLAG_PRESS_DOWN;

            btn->tick = 0;
            btn->click_count++;
        }
        else if (btn->tick > BTN_TIMEOUT_SHORT_PRESS)
        {
            if (btn->click_count == 1)
            {
                button_event_release(btn, BTN_FLAG_SINGLE_CLICK);
            }
            else if (btn->click_count == 2)
            {
                button_event_release(btn, BTN_FLAG_DOUBLE_CLICK);
            }

            btn->state = BTN_FLAG_IDLE;
        }
        break;

    case BTN_FLAG_LONG_PRESS_START:
        if (btn_status == BTN_STATUS_PRESS_UP)
        {
            button_event_release(btn, BTN_FLAG_PRESS_UP);
            btn->state = BTN_FLAG_IDLE;
        }
        else if (btn->tick > BTN_TIMEOUT_LONG_PRESS_HOLD)
        {
            button_event_release(btn, BTN_FLAG_LONG_PRESS_HOLD);
            btn->state = BTN_FLAG_LONG_PRESS_HOLD;
            btn->tick = 0;
        }
        break;

    case BTN_FLAG_LONG_PRESS_HOLD:
        if (btn_status == BTN_STATUS_PRESS_UP)
        {
            button_event_release(btn, BTN_FLAG_PRESS_UP);
            btn->state = BTN_FLAG_IDLE;
        }
        else if (btn->tick > BTN_TIMEOUT_HOLD_INTERVAL)
        {
            button_event_release(btn, BTN_FLAG_LONG_PRESS_HOLD);
            btn->tick = 0;
        }
        break;

    default:
        btn->state = BTN_FLAG_IDLE;
        break;
    }
}
